In this tutorial, we are going to use the method String.Format to format string for DateTime DataType.
Format a String for DateTime using C#
Example:
DateTime dateTime = DateTime.Now; String date; date = String.Format("Date & Time Format: {0}", dateTime); Console.WriteLine(date); date = String.Format("Date Only Format: {0:D}", dateTime); Console.WriteLine(date); date = String.Format("Time Only Format: {0:T}", dateTime); Console.WriteLine(date);
Output
Date time Format: 9/19/2019 6:01:19 PM
Date Only Format: Thursday, September 19, 2019
Time Only Format: 6:02:42 PM
C# Date Time Formats
There are different types of DateTime formats that you can use to format the given DateTime values in C#.
Character | Description | Usage | Example |
---|---|---|---|
D | Short Date | {0:d} | 09-19-2019 |
D | Long Date | {0:D} | Sep 19 2019 |
t or T | Long Date Time | {0:f} | Sep 19 2019 15:34:18 |
g or G | Short Date Time | {0:g} | 09-19-2019 15:34:18 |
M | Short Date | {0:M} | Sep 17 |
R | RFC1123 Date Time String | {0:r} | Thu , Sep 17 2019 15:34:18 GMT |
S | Sortable Date/Time | {0:s} | 2019-19-09T05:34:10 |
U | Universal Sortable Date | {0:u} | 2019-19-09 15:34:47Z |
1U | Universal full date | {0:U} | Sep 19 2019 04:18:07 |
Y | Year month pattern | {0:Y} | Sep, 2019 |
You can also use the date and time separators for better DateTime formation. For example:
DateTime dateTime = DateTime.Now; Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", dateTime)); Console.WriteLine(String.Format("{0:M/d/yyyy}", dateTime)); Console.WriteLine(String.Format("{0:MM/dd/yyyy}", dateTime)); Console.WriteLine(String.Format("{0:MM/dd/yy}", dateTime)); Console.WriteLine(String.Format("{0:ddd, MMM d, yyyy}", dateTime)); Console.WriteLine(String.Format("{0:dddd, MMMM d, yyyy}", dateTime));
Output
19/9/2019 19:29:34
9/19/2019
09/19/2019
09/19/19
Thu, Sep 19, 2019
Thursday, September 19, 2019
Standard DateTime format
There are some defined standard patterns for the DateTimeFormatInfo in the .NET Framework.
For example, the property ShortTimePattern
is a string that contains value h:mm for US time and HH:MM for DE time.
Character | DateTimeFromatInfo Property | Pattern |
---|---|---|
t | ShortTimePattern | h:mm tt |
d | ShortDatePattern | M/d/yyyy |
T | LongTimePattern | h:mm:ss tt |
D | LongDatePattern | dddd, MMMM dd, yyyy |
f | (combination of D and t) | dddd, MMMM dd, yyyy h:mm tt |
F | FullDateTimePattern | dddd, MMMM dd, yyyy h:mm:ss tt |
g | (combination of d and t) | M/d/yyyy h:mm tt |
G | (combination of d and T) | M/d/yyyy h:mm:ss tt |
m, M | MonthDayPattern | MMMM dd |
y, Y | YearMonthPattern | MMMM, yyyy |
r, R | RFC1123Pattern | ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ (*) |
s | SortableDateTimePattern | yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss (*) |
u | UniversalSortableDateTimePattern | yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ (*) |
Following are examples show the use of standard format specifiers
DateTime dateTime = DateTime.Now; Console.WriteLine(String.Format("{0:t}", dateTime)); Console.WriteLine(String.Format("{0:d}", dateTime)); Console.WriteLine(String.Format("{0:T}", dateTime)); Console.WriteLine(String.Format("{0:D}", dateTime)); Console.WriteLine(String.Format("{0:f}", dateTime)); Console.WriteLine(String.Format("{0:F}", dateTime)); Console.WriteLine(String.Format("{0:g}", dateTime)); Console.WriteLine(String.Format("{0:G}", dateTime)); Console.WriteLine(String.Format("{0:m}", dateTime)); Console.WriteLine(String.Format("{0:y}", dateTime)); Console.WriteLine(String.Format("{0:r}", dateTime)); Console.WriteLine(String.Format("{0:s}", dateTime)); Console.WriteLine(String.Format("{0:u}", dateTime));
Output
7:50 PM
9/19/2019
7:51:19 PM
Thursday, September 19, 2019
Thursday, September 19, 2019 7:51 PM
Thursday, September 19, 2019 7:52:12 PM
9/19/2019 7:52 PM
9/19/2019 7:52:55 PM
September 19
September, 2019
Thu, 19 Sep 2019 19:53:46 GMT
2019-09-19T19:54:09
2019-09-19 19:54:24Z
Related Articles
Comments